package com.oocl.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oocl.log4j.Log4j;
import com.oocl.util.JsonUtil;
import com.oocl.vo.MerchandiseVO;

/**
 * 从 session 加载出购物车的信息
 * @author XIEMI2
 *
 */
public class LoadCartItemsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		System.out.println("load items..........");
		HttpSession session = request.getSession();
		Map<String, MerchandiseVO> shoppingCart	= (HashMap<String, MerchandiseVO>) session.getAttribute("shoppingCart");
		Log4j.getLogger().info("java cart items "+JsonUtil.parserMerchandiseVOToString(shoppingCart));
//		response.getWriter().write(JsonUtil.parserMerchandiseVOToString(shoppingCart));
		response.getWriter().write(JsonUtil.parserMerchandiseVOToString((Map<String, MerchandiseVO>) session.getAttribute("shoppingCart")));
	}

}
